import Logger from "../utils/Logger"
import { GameMgr } from "./GameMgr"
import { Point, Terrain } from "./core/Point"

class Utils {

    getPointId(x:number,y:number) {
        return "("+x.toFixed(1)+","+y.toFixed(1)+")"
    }

    getObj(method:String,data:any = null,isGame:boolean = true,isLog:boolean = true) {
        return {"isGame":isGame,"isLog":isLog, "method":method,"data":data}
    }

    /** 获取周围一圈point */
    getArrPointsAround(gameMgr:GameMgr,point:Point){
        let position = point.position
        let arr_points = Array()
        let arr_pointIds = Array()
        arr_pointIds.push((this.getPointId(position.x-0.5,position.y+1)))
        arr_pointIds.push((this.getPointId(position.x+0.5,position.y+1)))
        arr_pointIds.push((this.getPointId(position.x-1,position.y)))
        arr_pointIds.push((this.getPointId(position.x+1,position.y)))
        arr_pointIds.push((this.getPointId(position.x-0.5,position.y-1)))
        arr_pointIds.push((this.getPointId(position.x+0.5,position.y-1)))
        for(let pointId of arr_pointIds){
            let p = gameMgr.map_point.get(pointId)
            if (p != null) {
                arr_points.push(p)
            }
        }
        return arr_points
    }

    /** 获取周围第二圈point */
    getArrPointsAround2(gameMgr:GameMgr,point:Point){
        let position = point.position
        let map_points = gameMgr.map_point
        let arr_points = Array()
        let arr_pointIds = Array()
        arr_pointIds.push((this.getPointId(position.x-1,position.y+2)))
        arr_pointIds.push((this.getPointId(position.x,position.y+2)))
        arr_pointIds.push((this.getPointId(position.x+1,position.y+2)))
        arr_pointIds.push((this.getPointId(position.x-1.5,position.y+1)))
        arr_pointIds.push((this.getPointId(position.x+1.5,position.y+1)))
        arr_pointIds.push((this.getPointId(position.x-2,position.y)))
        arr_pointIds.push((this.getPointId(position.x+2,position.y)))
        arr_pointIds.push((this.getPointId(position.x-1.5,position.y-1)))
        arr_pointIds.push((this.getPointId(position.x+1.5,position.y-1)))
        arr_pointIds.push((this.getPointId(position.x-1,position.y-2)))
        arr_pointIds.push((this.getPointId(position.x,position.y-2)))
        arr_pointIds.push((this.getPointId(position.x+1,position.y-2)))
        for (let pointId of arr_pointIds) {
            var p = map_points.get(pointId)
            if (p != null) {
                arr_points.push(p)
            }
        }
        Logger.log(arr_points)
        return arr_points
    }

    /** 获取第一圈+第二圈的point */
    getArrPointsAround1And2(gameMgr:GameMgr,point:Point){
        let arr_total = Array()
        let arr_points_1 = this.getArrPointsAround(gameMgr, point)
        let arr_points_2 = this.getArrPointsAround2(gameMgr, point)
        for (let p of arr_points_1) {
            arr_total.push(p)
        }
        for (let p of arr_points_2) {
            arr_total.push(p)
        }
        return arr_total
    }


    /** 秒数转时间 */
    secondToTime(second:number) {
        let minute = Math.floor(second/60)
        let str_minute = ""
        if (minute<10){
            str_minute = "0"+minute
        }else{
            str_minute = ""+minute
        }
        let remain = second%60
        let str_remain = ""
        if (remain<10){
            str_remain = "0"+remain
        }else{
            str_remain = ""+remain
        }
        return str_minute+":"+str_remain
    }

    /** 敲山震虎 获得的pointList */
    getArrPointsAsShan(gameMgr: GameMgr, point: Point, list: Array<Point>) {
        point.is_敲山震虎_flag = true
        let arr = this.getArrPointsAround(gameMgr, point)
        for (let p of arr) {
            if (p.terrain != Terrain.SHAN) {
                continue
            }
            if (p.is_敲山震虎_flag) {
                continue
            }
            this.getArrPointsAsShan(gameMgr, p, list)
        }
        return list
    }
} 
export default new Utils()
